package com.mfin.bom.factory;

import java.io.Serializable;

import com.mfin.bom.adapter.UserAdapter;
import com.mfin.bom.factory.interfaces.IUserInformationFactory;
import com.mfin.bom.interfaces.IUser;
import com.mfin.bom.objects.Finance;
import com.mfin.bom.objects.User;
import com.mfin.vo.ObjectKey;
import com.mfin.vo.interfaces.IIdentifiableObject;

public class UserInformationFactory extends BaseFactory implements
		IUserInformationFactory {

	public IUser getUserInformation(Serializable id) {
		IUser ui = null;

		try {
			User u = (User) getPersistenceGovernor().get(User.class, id);

			if (u != null) {
				ObjectKey[] keys = new ObjectKey[2];
				keys[1] = new ObjectKey(u.getId());

				Finance finance = u.getFinance();
				if (finance != null) {
					keys[0] = new ObjectKey(finance.getId());
				}
				ui = new UserAdapter(u, keys);

			}
		} catch (Exception ex) {
			// throw some exception
			ex.printStackTrace();
		}
		return ui;
	}

	public IUser getUserInformation(IIdentifiableObject identifiableObject) {
		IUser ui = null;

		ObjectKey[] keys = identifiableObject.getKeys();
		try {
			User u = (User) getPersistenceGovernor().get(User.class,
					keys[1].getId());

			if (u != null) {
				ui = new UserAdapter(u, keys);
			}
		} catch (Exception ex) {
			// throw some exception
			ex.printStackTrace();
		}
		return ui;
	}

}
